<?php 
  
 class ContactsWebModule extends WebModule 
 { 
    protected $id='contacts';
    protected static $defaultModel = 'ContactsDataModel';
    protected $feeds = array();

    protected function initialize() {
        $this->feeds = $this->loadFeedData();
        $this->maxPerPage = $this->getOptionalModuleVar('MAX_RESULTS', 10);
        if (count($this->feeds)==0) {
            return;
        }
    } 

    protected function getDefaultSection() {
        return key($this->feeds);
    }

    protected function getFeed($feed = null) {
        $feed = isset($this->feeds[$feed]) ? $feed : $this->getDefaultSection();
        $feedData = $this->feeds[$feed];
        $feedData['RETRIEVER_CLASS'] = 'ContactsDataRetriever';
        if ($feedData['BASE_XML'] != "") {
            $feedData['PARSER_CLASS'] = 'XMLDataParser';
            $feedData['BASE_URL'] = $feedData['BASE_XML'];
        } else {
            if ($feedData['FORMAT'] == 'json') {
                $feedData['PARSER_CLASS'] = 'JSONDataParser';
            } elseif ($feedData['FORMAT'] == 'xml') {
                $feedData['PARSER_CLASS'] = 'XMLDataParser';
            } elseif ($feedData['FORMAT'] == 'csv') {
                $feedData['PARSER_CLASS'] = 'CSVDataParser';
            }
        }
        $modelClass = isset($feedData['MODEL_CLASS']) ? $feedData['MODEL_CLASS'] : self::$defaultModel;
        $controller = DataModel::factory($modelClass, $feedData);
        return $controller;
    }

    protected function initializeForPage() { 

        if (count($this->feeds) == 0) {
            throw new KurogoConfigurationException($this->getLocalizedString('CONTACTS_FEED_CONFIGURE_ERROR_MESSAGE'));
        }

        switch ($this->page) 
        { 
            case 'index': 
                foreach($this->feeds as $feed => $feedData){
                    $list['title'] = $feedData['TITLE'];
                    $list['url'] = $this->buildBreadcrumbURL('list', array('id' => $feed), true);
                    $listArr[] = $list;
                }
                $this->assign('listArr', $listArr);
                break;
                
            case 'list':
                $id = $this->getArg(array('id'), null);
                $format = $this->feeds[$id]['FORMAT'];
                $title = $this->feeds[$id]['TITLE'];
                $controller = $this->getFeed($id);
                $name = $this->feeds[$id]['NAME'];
                $start = $this->getArg('start', 0);
                $controller->setStart($start);
                $controller->setLimit($this->maxPerPage);

                foreach ($controller->items() as $i => $test) {
                    $list['title'] = $test[$name];
                    $list['url'] = $this->buildBreadcrumbURL('detail', array('msg' => $id, 'id' => $start + $i), true);
                    $listArr[] = $list;
                }

                $totalItems = $controller->getTotalItems();
                $previousURL = null;
                $nextURL = null;

                if ($totalItems > $this->maxPerPage) {
                    $args = $this->args;
                    if ($start > 0) {
                        $args['start'] = $start - $this->maxPerPage;
                        $previousURL = $this->buildBreadcrumbURL($this->page, $args, false);
                    }

                    if (($totalItems - $start) > $this->maxPerPage) {
                        $args['start'] = $start + $this->maxPerPage;
                        $nextURL = $this->buildBreadcrumbURL($this->page, $args, false);
                    }
                }

                $this->assign('maxPerPage',     $this->maxPerPage);
                $this->assign('previousURL',    $previousURL);
                $this->assign('nextURL',        $nextURL);
                $this->assign('listArr', $listArr);
                $this->assign('title', $title);
                break;

            case 'detail':
                $id = $this->getArg(array('id'), null);
                $msg = $this->getArg(array('msg'), null);
                $str = $this->feeds[$msg]['FIELD'];
                $name = $this->feeds[$msg]['NAME'];
                $arr = explode(",", $str);
                $controller = $this->getFeed($msg);  	 
                $details = $controller->getDetail($id);

                foreach ($arr as $content) {
                    $fields = explode(":", $content);
                    if (isset($fields[1])) {
                        $key[$fields[0]] = array($details[$fields[0]], $fields[1]);
                    } else {
                        $key[$fields[0]] = array($details[$fields[0]], '');
                    }
                }
                $tabKeys = array('info');
                if (isset($this->feeds[$msg]['MAP_FIELD'])) {
                    $map = $this->feeds[$msg]['MAP_FIELD'];
                    $tabKeys[] = "map";
                    $this->assign('map', $key[$map][0]);
                }
                $this->assign('tabKeys', $tabKeys);
                $this->enableTabs($tabKeys);
                $this->assign('name', $key[$name][0]);
                $this->assign('keys', $key);
                break;
         } 
     } 
 }
